/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.StorageType;
import org.crystalrange.enterprise.service.StorageTypeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class StorageTypeListController extends BaseController {

    protected StorageTypeService storageTypeService;
    protected StorageType selectedStorageType;
    protected Listbox listboxStorageType;
    protected Search searchStorageType;
    protected List<StorageType> storageTypes;

    public StorageTypeService getStorageTypeService() {
        return this.storageTypeService;
    }

    public void setStorageTypeService(StorageTypeService storageTypeService) {
        this.storageTypeService = storageTypeService;
    }

    public StorageType getSelectedStorageType() {
        return this.selectedStorageType;
    }

    public void setSelectedStorageType(StorageType selectedStorageType) {
        this.selectedStorageType = selectedStorageType;
    }

    public List<StorageType> getStorageTypes() {
        return this.storageTypes;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchStorageType = new Search(StorageType.class);
        this.searchStorageType.setMaxResults(100);
        this.searchStorageType.addFetch("warehouseNumber");
        this.searchStorageType.addFetch("createdBy");
        this.searchStorageType.addFetch("updatedBy");
        this.storageTypes = this.storageTypeService.search(this.searchStorageType);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemStorageTypeCreate(Event event) throws Exception {
        this.storageTypeCreate();
    }

    public void onClick$menuitemStorageTypeEdit(Event event) throws Exception {
        this.storageTypeEdit();
    }

    public void onClick$menuitemStorageTypeDelete(Event event) throws Exception {
        this.storageTypeDelete();
    }

    public void onClick$menuitemStorageTypeSearch(Event event) throws Exception {
        this.storageTypeSearch();
    }

    public void onClick$menuitemStorageTypeRefresh(Event event) throws Exception {
        this.storageTypeRefresh();
    }

    public void onClick$toolbarbuttonStorageTypeCreate(Event event) throws Exception {
        this.storageTypeCreate();
    }

    public void onClick$toolbarbuttonStorageTypeEdit(Event event) throws Exception {
        this.storageTypeEdit();
    }

    public void onClick$toolbarbuttonStorageTypeDelete(Event event) throws Exception {
        this.storageTypeDelete();
    }

    public void onClick$toolbarbuttonStorageTypeSearch(Event event) throws Exception {
        this.storageTypeSearch();
    }

    public void onClick$toolbarbuttonStorageTypeRefresh(Event event) throws Exception {
        this.storageTypeRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.storageTypeEdit();
    }
    
    private void storageTypeCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/storageType.zul");
    }

    private void storageTypeEdit() throws Exception {
        if (this.selectedStorageType != null) {
            this.execution.sendRedirect("~./zul/html/storageType.zul?Id=" + this.selectedStorageType.getId().toString());
        }
    }

    private void storageTypeDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxStorageType.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    StorageType storageType = (StorageType) selectedItem.getValue();
                    this.storageTypeService.remove(storageType);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.storageTypes = this.storageTypeService.search(this.searchStorageType);
        this.binder.loadAll();
    }

    private void storageTypeSearch() throws Exception {
        this.execution.setAttribute("searchStorageType", this.searchStorageType);

        Window window = (Window) this.execution.createComponents("~./zul/html/storageTypeSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchStorageType = (Search) this.execution.getAttribute("searchStorageType");
        }

        this.storageTypes = this.storageTypeService.search(this.searchStorageType);
        this.binder.loadAll();
    }

    private void storageTypeRefresh() throws Exception {
        this.storageTypes = this.storageTypeService.search(this.searchStorageType);
        this.binder.loadAll();
    }
}
